package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.atguigu.tingshu.vo.order.TrackPayVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@PostMapping("/uploadTrack")
	private Result<UploadTrackVo> uploadTrack(MultipartFile file)
	{
	 UploadTrackVo uploadTrackVo =	trackInfoService.uploadTrack(file);
		return Result.build(uploadTrackVo , ResultCodeEnum.SUCCESS);
	}


	@GuiGuLogin
	@Operation(summary = "新增声音")
	@PostMapping("saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.saveTrackInfo(trackInfoVo, AuthContextHolder.getUserId());  //	调用服务层方法
		return Result.ok();
	}
	@GuiGuLogin
	@PostMapping(value = "/findUserTrackPage/{page}/{limit}")
	public Result<IPage<TrackListVo>>  findUserTrackPage(@PathVariable(value = "page") Long pageNo ,
														 @PathVariable(value = "limit") Long pageSize ,
														 @RequestBody TrackInfoQuery trackInfoQuery) {
		IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(pageNo , pageSize , trackInfoQuery) ;
		return Result.build(trackListVoIPage , ResultCodeEnum.SUCCESS) ;
	}

	@Operation(summary = "根据id查询声音信息")
	@GetMapping("getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getById(id);				//	调用服务层方法
		return Result.ok(trackInfo);
	}
	@Operation(summary = "修改声音")
	@PutMapping("updateTrackInfo/{id}")
	public Result updateById(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(id, trackInfoVo);	//	调用服务层方法
		return Result.ok();
	}

	@Operation(summary = "删除声音信息")
	@DeleteMapping("removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id) {
		trackInfoService.removeTrackInfo(id);			// 调用服务层方法
		return Result.ok();
	}
	@GuiGuLogin(required = false)
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(
												@PathVariable("albumId") Long albumId,
												@PathVariable("page")   Integer pageNo,
												@PathVariable("limit")   Integer pageSize) {
		IPage<AlbumTrackListVo> page = trackInfoService.findAlbumTrackPage(albumId , pageNo , pageSize);
		return Result.ok(page);
	}

	@Operation(summary = "获取声音统计信息")
	@GetMapping("getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}

	/**
	 * 根据用户所选择的声音的id查询要购买的声音的分集列表
	 */
	@GuiGuLogin
	@GetMapping(value = "/findUserTrackPaidList/{trackId}")
	public Result<List<TrackPayVo>> findUserTrackPaidList(@PathVariable(value = "trackId") Long trackId) {
		List<TrackPayVo> trackPayVoList = trackInfoService.findUserTrackPaidList(trackId) ;
		return Result.build(trackPayVoList , ResultCodeEnum.SUCCESS) ;
	}

	@GuiGuLogin
	// 根据当前用户所点击的声音的id，以及购买的声音的数量，查询下单声音列表数据
	@GetMapping(value = "/findPaidTrackInfoList/{trackId}/{number}")
	public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable(value = "trackId") Long trackId , @PathVariable(value = "number") Integer num) {
		List<TrackInfo> trackInfoList = trackInfoService.findPaidTrackInfoList(trackId , num) ;
		return Result.build(trackInfoList , ResultCodeEnum.SUCCESS) ;
	}
}

